<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_document.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_cart.php';
$title = 'Giỏ Hàng';


?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="uk-container uk-container-center uk-margin-top">
            <?php
            require_once DOCUMENT_ROOT . DIR_INCLUDE . 'menu.php';
            require_once DOCUMENT_ROOT . DIR_INCLUDE . 'slider.php';

            if (!empty($_POST['destroy'])) {
                $_SESSION['cart'][$_SESSION['user']] = array();
                lib_redirect::Redirect('/index.php');
            }
            
            if (empty($_SESSION['cart'][$_SESSION['user']]) || count($_SESSION['cart'][$_SESSION['user']]) == 0) {
                lib_redirect::Redirect('/index.php');
            }

            $remove_cart_id = 0;
            if (!empty($_POST['remove_id'])) {
                $remove_cart_id = $_POST['remove_id'];
            }
            api_cart::remove_doc_id_from_cart($remove_cart_id, "cart");
            ?>

            <section id="main-content" class="ts-body-container uk-panel uk-panel-box ts-padding-large">
                <form action="?" method="POST" id="form_destroy_cart">
                    <input type="hidden" name="destroy" value="1"/>
                </form>
                <form action="?" method="POST" id="form_remove_cart" >
                    <input type="hidden" name="remove_id" value="0" />
                </form>
                <table id="cart_table" class="uk-table uk-table-hover">
                    <caption>Danh sách bài tập muốn mua trong giỏ hàng</caption>
                    <thead>
                        <tr>
                            <th>STT</th>
                            <th>Mã bài tập</th>
                            <th>Tên bài tập</th>
                            <th>Giá</th>
                            <th class="uk-text-center">Xóa khỏi giỏ hàng</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $list = array();
                        if (!empty($_SESSION['cart'][$_SESSION['user']])) {
                            $list = $_SESSION['cart'][$_SESSION['user']];
                        }
                        $total_money = 0;
                        foreach ($list as $num => $item_id) {
                            $item = api_document::get_document_by_id($item_id);
                            $total_money += $item['doc_price'];
                        ?>
                        <tr>
                            <td><?php echo $num + 1; ?></td>
                            <td><?php echo $item['doc_id']; ?></td>
                            <td><?php echo $item['doc_name']; ?></td>
                            <td class="uk-text-bold"><?php echo number_format($item['doc_price'], 0, '.', ','); ?> vnđ</td>
                            <td class="uk-text-center">
                                <a href="javascript::void(0);" onclick="confirmRemoveCart('<?php echo $item['doc_id']; ?>');" style="padding: 10px;">
                                    <i class="uk-icon-ban"></i>
                                </a>
                            </td>
                        </tr>
                        <?php
                        }?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="3" class="uk-text-large">Tổng cộng:</td>
                            <td class="uk-text-bold uk-text-large"><?php echo number_format($total_money,0,'.',','); ?> vnđ</td>
                        </tr>
                    </tfoot>
                </table>
                <div class="uk-text-center">
                    <input type="submit" onclick="confirmDestroyCart();" name="btn_destroy" class="uk-button" value="Hủy giỏ hàng">
                    <input type="submit" name="btn_submit" onclick="return window.location = 'checkout.php#cart_table'" class="uk-button uk-button-primary" value="Tiếp theo">
                </div>
            </section>

            <?php
            require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
            ?>
        </div>
    </body>
</html>